home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-01-27 | 8.4 KB | 305 lines | [TEXT/CWIE] |
- /*
- File: ContextTypeCMPlugin.cp
-
- Contains: <contents>
-
- Written by: Guy Fullerton
-
- Copyright: <copyright>
-
- Change History (most recent first):
-
- <4> 1/16/97 GBF Updating for Direct to SOM. Adding fragment initializer.
- <3> 4/12/96 GBF The parameters for ExamineContext had changed.
- <2> 3/22/96 GBF Added outNeedMoreTime parameter to ExamineContext
- <1> 3/14/96 GBF first checked in
- */
-
-
- // Class Header
- #include "ContextTypeCMPlugin.h"
-
- // Mac OS Includes
- #include <AERegistry.h>
- #include <CodeFragments.h>
- #include <ContextualMenuPlugins.h>
-
- // SOM Includes
- #include <som.xh>
-
-
-
- // Function declarations
- extern pascal OSErr __initialize(CFragInitBlockPtr); // metrowerks's default initializer
- extern pascal void __terminate(void);
- pascal OSErr ContextTypeCMPluginInitialize(CFragInitBlockPtr init); // our initializer
- pascal void ContextTypeCMPluginTerminate(void); // our terminator
- OSStatus AddCommandToAEDescList(ConstStr255Param inCommandString,
- SInt32 inCommandID, AEDescList* ioCommandList);
- OSStatus CreateSampleSubmenu(AEDescList* ioCommandList);
-
-
-
- /*******************************************************************************
-
- ContextTypeCMPluginInitialize
-
- All plugin SOM object must somehow register themselves so clients
- can instantiate them by name. The best place that I've found to
- do it is in the fragment initializer.
-
- *******************************************************************************/
-
- pascal OSErr ContextTypeCMPluginInitialize(CFragInitBlockPtr init)
- {
- #pragma unused (init)
-
- OSErr theError = __initialize(init);
- if (theError == noErr)
- {
- // register our class with SOM
- somNewClass(ContextTypeCMPlugin);
- }
-
- return theError;
-
- } // ContextTypeCMPluginInitialize
-
-
-
- /*******************************************************************************
-
- ContextTypeCMPlugin::Initialize
-
- *******************************************************************************/
-
- OSStatus ContextTypeCMPlugin::Initialize(
- Environment*,
- FSSpec* inFileSpec)
- {
- #pragma unused (inFileSpec)
-
- // we don't need to do anything special here
-
- return noErr;
-
- } // ContextTypeCMPlugin::Initialize
-
-
-
- /*******************************************************************************
-
- ContextTypeCMPlugin::ExamineContext
-
- *******************************************************************************/
-
- OSStatus ContextTypeCMPlugin::ExamineContext(
- Environment*,
- AEDesc *inContextDescriptor,
- SInt32 inTimeOutInTicks,
- AEDescList* ioCommands,
- Boolean* outNeedMoreTime)
- {
- #pragma unused(inTimeOutInTicks)
-
- // this is a quick sample that looks for text in the context descriptor
-
- // make sure the descriptor isn't null
- if (inContextDescriptor != NULL)
- {
- // tell the raw type of the descriptor
- Str15 theDescriptorType;
- theDescriptorType[0] = 4;
- *(DescType*)(&theDescriptorType[1]) = inContextDescriptor->descriptorType;
- ::AddCommandToAEDescList(theDescriptorType, 1, ioCommands);
-
- // try to get text out of the context descriptor; make sure to
- // coerce it, cuz the app may have passed an object specifier or
- // styled text, etc.
- AEDesc theTextDesc = { typeNull, NULL };
- if (::AECoerceDesc(inContextDescriptor, typeChar, &theTextDesc) == noErr)
- {
- // add a text only command to our command list
- ::AddCommandToAEDescList("\pWe got text!", 2, ioCommands);
- }
- ::AEDisposeDesc(&theTextDesc);
-
- // Just for kicks, lets create a submenu for our plugin
- ::CreateSampleSubmenu(ioCommands);
- }
- else
- {
- // we have a null descriptor
- ::AddCommandToAEDescList("\pNULL Descriptor", 3, ioCommands);
- }
-
- *outNeedMoreTime = false;
-
- return noErr;
-
- } // ContextTypeCMPlugin::ExamineContext
-
-
-
- /*******************************************************************************
-
- ContextTypeCMPlugin::HandleSelection
-
- *******************************************************************************/
-
- OSStatus ContextTypeCMPlugin::HandleSelection(
- Environment*,
- AEDesc *inContextDescriptor,
- SInt32 inCommandID)
- {
- #pragma unused (inContextDescriptor, inCommandID)
-
- // here is where you would actually carry out the action that the user
- // requested.
-
- return noErr;
-
- } // ContextTypeCMPlugin::HandleSelection
-
-
-
- /*******************************************************************************
-
- ContextTypeCMPlugin::PostMenuCleanup
-
- *******************************************************************************/
-
- OSStatus ContextTypeCMPlugin::PostMenuCleanup(
- Environment*)
- {
- // we didn't allocate any buffers or cache any data in ExamineContext,
- // so we don't need to clean anything up here
-
- return noErr;
-
- } // ContextTypeCMPlugin::PostMenuCleanup
-
-
-
- /*******************************************************************************
-
- AddCommandToAEDescList
-
- *******************************************************************************/
-
- OSStatus AddCommandToAEDescList(
- ConstStr255Param inCommandString,
- SInt32 inCommandID,
- AEDescList* ioCommandList)
- {
- OSStatus theError = noErr;
-
- AERecord theCommandRecord = { typeNull, NULL };
-
- do
- {
- // create an apple event record for our command
- theError = ::AECreateList(NULL, 0, true, &theCommandRecord);
- if (theError != noErr) break;
-
- // stick the command text into the aerecord
- theError = ::AEPutKeyPtr(&theCommandRecord, keyAEName, typeChar,
- &inCommandString[1], inCommandString[0]);
- if (theError != noErr) break;
-
- // stick the command ID into the AERecord
- theError = ::AEPutKeyPtr(&theCommandRecord, keyContextualMenuCommandID, typeLongInteger,
- &inCommandID, sizeof (inCommandID));
- if (theError != noErr) break;
-
- // stick this record into the list of commands that we are passing back to CMM
- theError = ::AEPutDesc(ioCommandList, // the list we're putting our command into
- 0, // stick this command onto the end of our list
- &theCommandRecord); // the command I'm putting into the list
-
- } while (false);
-
- // clean up after ourself; dispose of the AERecord
- AEDisposeDesc(&theCommandRecord);
-
- return theError;
-
- } // AddCommandToAEDescList
-
-
-
- /*******************************************************************************
-
- CreateSampleSubmenu
-
- *******************************************************************************/
-
- OSStatus CreateSampleSubmenu(AEDescList* ioCommandList)
- {
- OSStatus theError = noErr;
-
- AEDescList theSubmenuCommands = { typeNull, NULL };
- AERecord theSupercommand = { typeNull, NULL };
-
- do
- {
- // the first thing we should do is create an AEDescList of
- // subcommands
- {
- // set up the AEDescList
- theError = ::AECreateList(NULL, 0, false, &theSubmenuCommands);
- if (theError != noErr) break;
-
- // stick some commands in this subcommand list
- theError = ::AddCommandToAEDescList("\pSubcommand 1", 1001, &theSubmenuCommands);
- if (theError != noErr) break;
-
- // another
- theError = ::AddCommandToAEDescList("\pAnother Subcommand", 1002, &theSubmenuCommands);
- if (theError != noErr) break;
-
- // yet another
- theError = ::AddCommandToAEDescList("\pLast One", 1003, &theSubmenuCommands);
- if (theError != noErr) break;
- }
-
- // now, we need to create the supercommand which will "own" the
- // subcommands. The supercommand lives in the root command list.
- // this looks very much like the AddCommandToAEDescList function,
- // except that instead of putting a command ID in the record,
- // we put in the subcommand list.
- {
- // create an apple event record for our supercommand
- theError = ::AECreateList(NULL, 0, true, &theSupercommand);
- if (theError != noErr) break;
-
- // stick the command text into the aerecord
- Str255 theSupercommandText = "\pSubmenu Here";
- theError = ::AEPutKeyPtr(&theSupercommand, keyAEName, typeChar,
- &theSupercommandText[1], theSupercommandText[0]);
- if (theError != noErr) break;
-
- // stick the subcommands into into the AERecord
- theError = ::AEPutKeyDesc(&theSupercommand, keyContextualMenuSubmenu,
- &theSubmenuCommands);
- if (theError != noErr) break;
-
- // stick the supercommand into the list of commands that we are passing back to CMM
- theError = ::AEPutDesc(ioCommandList, // the list we're putting our command into
- 0, // stick this command onto the end of our list
- &theSupercommand); // the command I'm putting into the list
- }
- }
- while (false);
-
- // clean up after ourself
- AEDisposeDesc(&theSubmenuCommands);
- AEDisposeDesc(&theSupercommand);
-
- return theError;
-
- } // CreateSampleSubmenu
-
-
-
-